<template>
	<view>
		<view class=" content posRelative flex-col align-center">
			<view class="align-center ft24">
				<!-- <image src="../../static/images/logo.jpg" style="width: 600rpx;height: 134rpx; "  mode="widthFix"></image> -->

			</view>


			<view class="" style="height: 200rpx;">

			</view>
			<view v-if="loginType!='1'" class="mt20 mb15" style="width: 600rpx;">
				<uv-input prefixIconStyle="font-size: 22px;color: #909399" placeholder="请输入手机号/账号" border="surround"
					v-model="username">
					<template v-slot:prefix>
						<view class="align-center">
							<uv-icon name="account"></uv-icon>账号
						</view>
					</template>
				</uv-input>

			</view>
			<view v-if="loginType!='1'" class=" mb15" style="width: 600rpx;">
				
				<uv-input prefixIcon="lock" prefixIconStyle="font-size: 22px;color: #909399" :password="true"
					placeholder="请输入密码" border="surround" v-model="password">
					
					<template v-slot:prefix>
						<view class="align-center">
							<uv-icon name="lock"></uv-icon>密码
						</view>
					</template>
					</uv-input>

			</view>
			<button class="submit" @click="login">学员登录</button>
			<!-- 		<button class="submit" v-if="loginType==1" open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">手机号快捷登录</button> -->
			<!-- <button class="submit" open-type="getUserInfo" @getuserinfo="getUserInfo"  >手机号快捷登录</button> -->
			<!-- <view class="loginstatus" @click="changetype">
				{{loginType==1? '使用账号密码登录':'手机号快捷登录'}}
			</view> -->
			<!-- <view class="xy align-center justify-center">
				<uv-checkbox-group v-model="checked">
					<uv-checkbox shape="circle" :customStyle="{
           		'margin':'0 auto'
           	}" activeColor="#0368FF" name="apple" label="">
						<template #default>
							<view class="align-center justify-center ft13" style="color: #999;">
								我已阅读并接受<text @click.stop="toxy(3)" style="color: #0368FF;">《用户协议》</text> 及<text
									@click.stop="toxy(2)" style="color: #0368FF;">《隐私协议》</text>
							</view>
						</template>

					</uv-checkbox>
				</uv-checkbox-group>
			</view> -->


		</view>
	</view>
</template>

<script>
	import operate from '@/common/operate.js'
	export default {
		data() {
			return {
				form: {
					openid: "",
					nickname: "",
					avatar: "",
					phone: ""
				},
				checked: [],
				code: "",
				value: "",
				pwd: '',
				loginType: "",
				username: "",
				password: ""

			}
		},
		onLoad() {
			uni.removeStorageSync('logintoken')
			uni.removeStorageSync('userId')
			uni.removeStorageSync('userInfo')
			// this.getSessionKey()
		},
		mounted() {
			// this.$refs.popup.open()
		},
		methods: {
			changetype() {
				this.loginType = this.loginType == '1' ? '' : '1'
			},
			async getSessionKey() {
				let vm = this
				uni.login({
					provider: 'weixin',
					"onlyAuthorize": true,
					success: async function(data) {
						console.log(data)
						vm.code = data.code
					},
					fail: function(err) {

						console.log('登录授权失败', err.code)
					},
				});
			},
			getUserInfo(event) {
				let vm = this
				this.$api.login({
					code: this.code,
					iv: event.detail.iv,
					encryptedData: event.detail.encryptedData,
				}).then(res => {
					if (res.code == 1) {
						vm.$utils.msg('登录成功')
						uni.setStorageSync('userInfo', res.data.userinfo)
						uni.setStorageSync('userId', res.data.userinfo.userId)
						uni.setStorageSync('unionId', res.data.unionId)
						uni.setStorageSync('logintoken', res.data.authorization)
						uni.setStorageSync('menus', res.data.permissions)
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/home/index'
							})
						}, 800)

					} else {
						vm.$utils.msg(res.message)
					}

				})

			},
			login() {
				let vm = this
				if (!this.username) return this.$utils.msg('请输入账号')
				if (!this.password) return this.$utils.msg('请输入密码')
				// if (!this.checked.length) return this.$utils.msg('请阅读并勾选用户协议和隐私协议')
				this.$api.login({

					account: vm.username,
					password: vm.password
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						vm.$utils.msg('登录成功')
						uni.setStorageSync('userInfo', res.data.userinfo)
						
						uni.setStorageSync('logintoken', res.data.userinfo.token)

						
						
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 800)
					} else {
						vm.$utils.msg(res.msg)
					}
				})

			},
			// async getUserInfo(userid){
			// 	let vm =this
			//  const {data} =  await this.$api.getMyInfo({user_id:userid})
			//  console.log(data)

			//  vm.$utils.msg('登录成功')
			//  uni.setStorageSync('userInfo', data)
			//  setTimeout(()=>{
			// 	 uni.switchTab({
			// 	     url:'/pages/tabbar/mine'
			// 	 })
			//  },800)


			// },
			getPhoneNumber(event) {
				console.log(event)
				let vm = this
				this.$api.wxLogin({
					code: this.code,
					phoneIv: event.detail.iv,
					phoneData: event.detail.encryptedData,


				}).then(res => {
					console.log(res)
					vm.$utils.msg('登录成功')
					uni.setStorageSync('userInfo', res.data)
					uni.setStorageSync('userId', res.data.userId)
					uni.setStorageSync('unionId', res.data.unionId)

					uni.setStorageSync('logintoken', res.data.authorization)
					uni.setStorageSync('menus', res.data.menus)
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}, 800)
				})

			},
			async chooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail
				console.log(avatarUrl)
				const result = await this.$api.uploadImg({
					filePath: avatarUrl
				})
				console.log(result)
				this.form.avatar = operate.api + 'storage/' + result.filename
			},




		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.content {
		padding-top: 150upx;
		background-color: #ffffff;
		height: 100vh;
	}

	.avatar-box {
		width: 96rpx;
		height: 96rpx;
		background: transparent;
		border: none;
		border-radius: 50%;

		image {
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}

		&::after {
			display: none;
		}
	}

	.title {
		font-size: 32rpx;
		text-align: center;
		margin: 30rpx auto 60rpx;
		font-weight: 600;
	}

	.submit {
		position: fixed;
		left: 0;
		right: 0;
		margin: auto;
		bottom: 400rpx;
		width: 600rpx;
		height: 100rpx;
		margin-top: 300rpx;
		background: #0368FF;
		border-radius: 15rpx;
		font-size: 32rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 100rpx;
	}

	.xy {
		position: fixed;
		bottom: 100rpx;
	}

	.loginstatus {
		color: #0368FF;
		position: fixed;
		left: 0;
		right: 0;
		margin: auto;
		text-align: center;
		bottom: 250rpx;
	}
</style>